package com.example.hyznbk.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;


import com.example.hyznbk.R;

/**
 * Created by tangminglong on 16/12/8.
 */

public class PopupMenuType1Adapter extends BasePopupMenuAdapter {

    private final Context context;
    private String[] learnSubjects ;
    private int checkPosition = 0;

    public PopupMenuType1Adapter(Context context) {
        this.context = context;
    }

    public PopupMenuType1Adapter(Context context,String[] learnSubjects) {
        this.context = context;
        this.learnSubjects = learnSubjects;
    }

    @Override
    public int getCount() {
        return learnSubjects.length;
    }

    @Override
    public String getItem(int position) {
        return learnSubjects[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_popupmenu,parent,false);
            holder = new ViewHolder();
            holder.tv_subject = (TextView) convertView.findViewById(R.id.tv_popupmenu);
            holder.cb_subject = (CheckBox) convertView.findViewById(R.id.cb_popupmenu);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == checkPosition){
            holder.cb_subject.setChecked(true);
            holder.cb_subject.setVisibility(View.VISIBLE);
            holder.tv_subject.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        }else {
            holder.cb_subject.setChecked(false);
            holder.cb_subject.setVisibility(View.INVISIBLE);
            holder.tv_subject.setTextColor(context.getResources().getColor(R.color.radio_normal));
        }
        holder.tv_subject.setText(learnSubjects[position]);


        return convertView;
    }

    @Override
    public void setCheckPosition(int position) {
        checkPosition = position;
        notifyDataSetChanged();
    }

    @Override
    public String getCheckPositionStr() {
        return learnSubjects[checkPosition];
    }

    @Override
    public int getCheckPositionIndex() {
        return checkPosition;
    }

    private class ViewHolder{
        public TextView tv_subject;
        public CheckBox cb_subject;
    }

    public void setData(String[] list){

        checkPosition = 0;
        this.learnSubjects = list;
        notifyDataSetChanged();
    }
}
